package com.sunshine.lnuplus.adapter

import android.content.Context
import android.graphics.BitmapFactory
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.viewpager.widget.PagerAdapter
import com.bumptech.glide.Glide
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.callback.RecyclerViewCallBack

class TimetableThemeAdapter(
    private val context: Context,
    private val param: RecyclerViewCallBack
) : PagerAdapter() {

    override fun getCount(): Int {
        return 4
    }

    override fun isViewFromObject(
        view: View,
        `object`: Any
    ): Boolean {
        return view === `object`
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val constraintLayout = LayoutInflater.from(container.context).inflate(
            R.layout.item_timetable_theme,
            null
        ) as ConstraintLayout
        constraintLayout.id = R.id.item_id
        val name: TextView = constraintLayout.findViewById(R.id.text_theme_name)
        when (position) {
            0 -> {
                val imageView: ImageView = constraintLayout.findViewById(R.id.theme_background)
                Glide
                    .with(context)
                    .load(context.resources.getDrawable(R.mipmap.timetable_white))
                    .into(imageView)
                name.text = "纯色"
            }
            1 -> {
                val imageView: ImageView = constraintLayout.findViewById(R.id.theme_background)
                Glide
                    .with(context)
                    .load(context.resources.getDrawable(R.mipmap.timetable_image))
                    .into(imageView)
                name.text = "默认背景"
            }
            2 -> {
                val imageView: ImageView = constraintLayout.findViewById(R.id.theme_background)
                Glide
                    .with(context)
                    .load(context.resources.getDrawable(R.mipmap.timetable_image))
                    .into(imageView)
                name.text = "每日一图"
            }
            3 -> {
                val imageView: ImageView = constraintLayout.findViewById(R.id.theme_background)
                Glide
                    .with(context)
                    .load(BitmapFactory.decodeResource(context.resources, R.mipmap.timetable_image))
                    .into(imageView)
                name.text = "自定义"
            }
        }
        constraintLayout.setOnClickListener {
            param.onChanged(position)
        }
        container.addView(constraintLayout)
        return constraintLayout
    }

    override fun destroyItem(
        container: ViewGroup,
        position: Int,
        `object`: Any
    ) {
        val view = `object` as ConstraintLayout
        container.removeView(view)
    }

}